package com.explorati.LeetCode125.validpalindrome;

public class Solution1 {
	public static boolean isPalindrome(String s) {
		if (s == "") {
			return true;
		}
		boolean flag = true;
		String str = s.toLowerCase();
		int i = 0, j = s.length() - 1;
		char cHead, cTail;
		while (i <= j) {
			cHead = str.charAt(i);
			cTail = str.charAt(j);
			while (!Character.isLetterOrDigit(cHead)) {
				if (i < s.length() - 1) {
					cHead = str.charAt(++i);
				}
			}
			while (!Character.isLetterOrDigit(cTail)) {
				if (j > 0) {
					cTail = str.charAt(--j);
				}
			}
			if (i >= j) {
				return true;
			}
			if (cHead != cTail) {
				return false;
			}
			i++;
			j--;
		}
		return flag;
		// String str = s.toLowerCase().replaceAll("[^a-z0-9]", "");
		// return new StringBuilder(str).reverse().toString().equals(s);
	}

	public static void main(String[] args) {
		// String s = "A man, a plan, a canal: Panama";
		 String s = "race a car";
		// String s = "0P";

		System.out.println(isPalindrome(s));
	}
}
